package com.adingxiong.cft.util;

import com.adingxiong.cft.errors.ApiException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName CommonUtils
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/5/11 11:22
 **/
public class CommonUtils {

    public static Instant toInstant(String datetime) {
        return LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toInstant(ZoneOffset.ofHours(8));
    }

    public static Map<String, Object> getResponse(Long id) {
        Map<String, Object> res = new HashMap<>();
        res.put("id", id);
        return res;
    }

    @SafeVarargs
    public static <E, M> List<String> map(Collection<E> entries, Function<E, M>... getters) {
        if (CollectionUtils.isEmpty(entries)) {
            return Collections.emptyList();
        }
        Assert.notEmpty(getters, "At least 1 elements in the \"getters\" parameter");
        List<M> values = new ArrayList<>();
        entries.forEach(e -> Stream.of(getters).map(getter -> getter.apply(e)).forEach(values::add));
        return values.parallelStream().filter(Objects::nonNull).distinct().map(Objects::toString).collect(Collectors.toList());
    }

    @SafeVarargs
    public static <E, M> String groupConcat(Collection<E> entries, String delimiter, boolean quote, Function<E, M>... getters) {
        return map(entries, getters)
                .parallelStream()
                .map(s -> quote ? StringUtils.quote(s) : s)
                .collect(Collectors.joining(delimiter));
    }

    @SafeVarargs
    public static <E, M> String groupConcat(Collection<E> entries, boolean quote, Function<E, M>... getters) {
        return groupConcat(entries, ",", quote, getters);
    }

    @SafeVarargs
    public static <E, M> String groupConcat(Collection<E> entries, Function<E, M>... getters) {
        return groupConcat(entries, false, getters);
    }

    /*@SafeVarargs
    public static <E, M> Map<Integer, String> getUsernames(UserService service, Collection<E> entries, Function<E, M>... getters) {
        String userIds = groupConcat(entries, getters);
        return StringUtils.isEmpty(userIds) ? Collections.emptyMap() : service.getUserNamesByUserIds(userIds);
    }

    @SafeVarargs
    public static <E, M> Map<Integer, String> getOrgNames(OrganizationService service, Collection<E> entries, Function<E, M>... getters) {
        String orgIds = groupConcat(entries, getters);
        return StringUtils.isEmpty(orgIds) ? Collections.emptyMap() : service.getOrgNamesByOrgIds(orgIds);
    }

    @SafeVarargs
    public static <E, M> Map<Integer, String> getOrgChainNames(OrganizationService service, Collection<E> entries, Function<E, M>... getters) {
        String orgIds = groupConcat(entries, getters);
        return StringUtils.isEmpty(orgIds) ? Collections.emptyMap() : service.getChainNamesByIds(orgIds);
    }

    @SafeVarargs
    public static <E, M> Map<Integer, String> getDictNames(DictService dictService, Collection<E> entries, Function<E, M>... getters) {
        String ids = groupConcat(entries, getters);
        return StringUtils.isEmpty(ids) ? Collections.emptyMap() : dictService.getDictNamesByIds(ids);
    }*/
}
